package info.openlvb.openface.client.base.home;

import info.openlvb.openface.client.handler.CallPersoGraphListenner;
import info.openlvb.openface.client.service.GraphService;
import info.openlvb.openface.client.service.GraphServiceAsync;

import java.util.Date;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.widgets.ToolTip;
import com.gwtext.client.widgets.portal.Portlet;

public class RRDService {
	private final GraphServiceAsync GraphService = GWT.create(GraphService.class);
	private FlexTable table = null;
	private VerticalPanel vp = new VerticalPanel();
	
	public RRDService(Portlet portlet){
		
		CallPersoGraphListenner handler = new CallPersoGraphListenner();
		
		vp.setBorderWidth(1);
		vp.setSpacing(1);
		vp.setWidth("361px");
		final Image img1 = new Image();
		img1.setUrl("images/cpu-3h.png");
		img1.setTitle("cpu");
		img1.setSize("350px","125px");
		final Image img2 = new Image();
		img2.setUrl("images/mem-3h.png");
		img2.setTitle("mem");
		img2.setSize("350px","125px");
		final Image img3 = new Image();
		img3.setUrl("images/net-1h.png");
		img3.setTitle("net");
		img3.setSize("350px","125px");
		final Image rondVert = new Image();
		rondVert.setUrl("images/vert.jpg");
		final Image rondRouge = new Image();
		rondRouge.setUrl("images/rouge.gif");
		table = new FlexTable();
		table.setHTML(0, 0, "- cron : ");
		testService();
		vp.add(table);
		img1.addClickHandler(handler);
		img2.addClickHandler(handler);
		img3.addClickHandler(handler);
		ToolTip tip1 = new ToolTip();  
		tip1.setHtml("Cliquer sur le graphe pour le personnaliser");
		tip1.setDismissDelay(2000);
		tip1.setAutoShow(true);
		tip1.setTrackMouse(true);
		tip1.applyTo(portlet);
		vp.add(img1);
		vp.add(img2);
		vp.add(img3);

		Timer timer = new Timer() {
			@Override
			public void run() {

				img1.setUrl("images/cpu-3h.png?"+new Date().getTime());
				img2.setUrl("images/mem-3h.png?"+new Date().getTime());
				img3.setUrl("images/net-1h.png?"+new Date().getTime());
			}
		};
		timer.scheduleRepeating(60000);
	}

	private void testService(){
		GraphService.pid("/var/run/crond.pid",new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
				simplePopup.setWidget(new HTML("probleme de graphService"));
				simplePopup.center();
				simplePopup.show();
			}
			public void onSuccess(Boolean result) {
				if (result) table.setHTML(0, 1,"<img src=\"images/vert.jpg\"\\>");
				else table.setHTML(0, 1,"<img src=\"images/rouge.gif\"\\>");
			}
		}

		);

	}

	public VerticalPanel getVp() {
		return vp;
	}

	public void setVp(VerticalPanel vp) {
		this.vp = vp;
	}
}
